package generalpattern.diagram.edit.parts;

import generalpattern.diagram.edit.policies.Overlapping2ItemSemanticEditPolicy;
import generalpattern.diagram.providers.GeneralPatternMetamodelElementTypes;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.lite.svg.SVGFigure;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.graphics.Color;

/**
 * @generated
 */
public class Overlapping2EditPart extends ShapeNodeEditPart {

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 2027;

	/**
	 * @generated
	 */
	protected IFigure contentPane;

	/**
	 * @generated
	 */
	protected IFigure primaryShape;

	/**
	 * @generated
	 */
	public Overlapping2EditPart(View view) {
		super(view);
	}

	/**
	 * @generated
	 */
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
				new Overlapping2ItemSemanticEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
		// XXX need an SCR to runtime to have another abstract superclass that would let children add reasonable editpolicies
		// removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.CONNECTION_HANDLES_ROLE);
	}

	/**
	 * @generated
	 */
	protected LayoutEditPolicy createLayoutEditPolicy() {
		org.eclipse.gmf.runtime.diagram.ui.editpolicies.LayoutEditPolicy lep = new org.eclipse.gmf.runtime.diagram.ui.editpolicies.LayoutEditPolicy() {

			protected EditPolicy createChildEditPolicy(EditPart child) {
				EditPolicy result = child
						.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
				if (result == null) {
					result = new NonResizableEditPolicy();
				}
				return result;
			}

			protected Command getMoveChildrenCommand(Request request) {
				return null;
			}

			protected Command getCreateCommand(CreateRequest request) {
				return null;
			}
		};
		return lep;
	}

	/**
	 * @generated
	 */
	protected IFigure createNodeShape() {
		return primaryShape = new OverlappingFigure();
	}

	/**
	 * @generated
	 */
	public OverlappingFigure getPrimaryShape() {
		return (OverlappingFigure) primaryShape;
	}

	/**
	 * @generated
	 */
	protected NodeFigure createNodePlate() {
		DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(23, 23);
		return result;
	}

	/**
	 * Creates figure for this edit part.
	 * 
	 * Body of this method does not depend on settings in generation model
	 * so you may safely remove <i>generated</i> tag and modify it.
	 * 
	 * @generated
	 */
	protected NodeFigure createNodeFigure() {
		NodeFigure figure = createNodePlate();
		figure.setLayoutManager(new StackLayout());
		IFigure shape = createNodeShape();
		figure.add(shape);
		contentPane = setupContentPane(shape);
		return figure;
	}

	/**
	 * Default implementation treats passed figure as content pane.
	 * Respects layout one may have set for generated figure.
	 * @param nodeShape instance of generated figure class
	 * @generated
	 */
	protected IFigure setupContentPane(IFigure nodeShape) {
		return nodeShape; // use nodeShape itself as contentPane
	}

	/**
	 * @generated
	 */
	public IFigure getContentPane() {
		if (contentPane != null) {
			return contentPane;
		}
		return super.getContentPane();
	}

	/**
	 * @generated
	 */
	protected void setForegroundColor(Color color) {
		if (primaryShape != null) {
			primaryShape.setForegroundColor(color);
		}
	}

	/**
	 * @generated
	 */
	protected void setBackgroundColor(Color color) {
		if (primaryShape != null) {
			primaryShape.setBackgroundColor(color);
		}
	}

	/**
	 * @generated
	 */
	protected void setLineWidth(int width) {
		if (primaryShape instanceof Shape) {
			((Shape) primaryShape).setLineWidth(width);
		}
	}

	/**
	 * @generated
	 */
	protected void setLineType(int style) {
		if (primaryShape instanceof Shape) {
			((Shape) primaryShape).setLineStyle(style);
		}
	}

	/**
	 * @generated
	 */
	public List/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/getMARelTypesOnSource() {
		ArrayList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/types = new ArrayList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/(
				2);
		types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		return types;
	}

	/**
	 * @generated
	 */
	public List/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/getMARelTypesOnSourceAndTarget(
			IGraphicalEditPart targetEditPart) {
		LinkedList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/types = new LinkedList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/();
		if (targetEditPart instanceof Pattern2EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof ComponentEditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof ConnectorEditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof Component2EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof ComponentPortEditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof Component3EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof Connector2EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof ConnectorRoleEditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof Pattern3EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof Connector3EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof Pattern4EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017);
		}
		if (targetEditPart instanceof Pattern2EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		if (targetEditPart instanceof ComponentEditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		if (targetEditPart instanceof ConnectorEditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		if (targetEditPart instanceof Component2EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		if (targetEditPart instanceof ComponentPortEditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		if (targetEditPart instanceof Component3EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		if (targetEditPart instanceof Connector2EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		if (targetEditPart instanceof ConnectorRoleEditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		if (targetEditPart instanceof Pattern3EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		if (targetEditPart instanceof Connector3EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		if (targetEditPart instanceof Pattern4EditPart) {
			types.add(GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018);
		}
		return types;
	}

	/**
	 * @generated
	 */
	public List/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/getMATypesForTarget(
			IElementType relationshipType) {
		LinkedList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/types = new LinkedList/*[org.eclipse.gmf.runtime.emf.type.core.IElementType]*/();
		if (relationshipType == GeneralPatternMetamodelElementTypes.MergingOperatorSource_4017) {
			types.add(GeneralPatternMetamodelElementTypes.Pattern_2022);
			types.add(GeneralPatternMetamodelElementTypes.Component_2023);
			types.add(GeneralPatternMetamodelElementTypes.Connector_2024);
			types.add(GeneralPatternMetamodelElementTypes.Component_3025);
			types.add(GeneralPatternMetamodelElementTypes.ComponentPort_3026);
			types.add(GeneralPatternMetamodelElementTypes.Component_3027);
			types.add(GeneralPatternMetamodelElementTypes.Connector_3028);
			types.add(GeneralPatternMetamodelElementTypes.ConnectorRole_3029);
			types.add(GeneralPatternMetamodelElementTypes.Pattern_3030);
			types.add(GeneralPatternMetamodelElementTypes.Connector_3031);
			types.add(GeneralPatternMetamodelElementTypes.Pattern_3032);
		} else if (relationshipType == GeneralPatternMetamodelElementTypes.MergingOperatorTarget_4018) {
			types.add(GeneralPatternMetamodelElementTypes.Pattern_2022);
			types.add(GeneralPatternMetamodelElementTypes.Component_2023);
			types.add(GeneralPatternMetamodelElementTypes.Connector_2024);
			types.add(GeneralPatternMetamodelElementTypes.Component_3025);
			types.add(GeneralPatternMetamodelElementTypes.ComponentPort_3026);
			types.add(GeneralPatternMetamodelElementTypes.Component_3027);
			types.add(GeneralPatternMetamodelElementTypes.Connector_3028);
			types.add(GeneralPatternMetamodelElementTypes.ConnectorRole_3029);
			types.add(GeneralPatternMetamodelElementTypes.Pattern_3030);
			types.add(GeneralPatternMetamodelElementTypes.Connector_3031);
			types.add(GeneralPatternMetamodelElementTypes.Pattern_3032);
		}
		return types;
	}

	/**
	 * @generated
	 */
	protected void handleNotificationEvent(Notification event) {
		if (event.getNotifier() == getModel()
				&& EcorePackage.eINSTANCE.getEModelElement_EAnnotations()
						.equals(event.getFeature())) {
			handleMajorSemanticChange();
		} else {
			super.handleNotificationEvent(event);
		}
	}

	/**
	 * @generated
	 */
	public class OverlappingFigure extends SVGFigure {

		/**
		 * @generated
		 */
		public OverlappingFigure() {
			this.setURI("platform:/plugin/org.admanager.generalpattern.plugin/icons/full/obj16/overlapping.svg");
			this.setPreferredSize(new Dimension(getMapMode().DPtoLP(23),
					getMapMode().DPtoLP(23)));
		}

	}

}
